﻿package com.cilico.single.view;

import android.os.Bundle;

import com.cilico.single.ConfigManager;
import com.cilico.single.components.AtomParser;
import com.cilico.single.components.Label;
import com.cilico.single.components.WaitDialog;
import com.cilico.single.utils.ConstantRepository;
import com.cilico.single.utils.Utils;

/**
 * 帮助
 * @author lv.xiangan
 *
 */
public class HelpView extends BaseView {
	public String helpInfo_;
	String[][] helpCatalog_;
	boolean isInSubContent_;
	String helpSubContent_;

	Label log_;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activities_.add(this);
		int roientation = this.getResources().getConfiguration().orientation;
		// 横竖屏切换时调用此方法交换宽高
		changeScreenWidthHeight(roientation);
		// 进入界面后立即复制当前界面对象，保存当前界面
		ConfigManager.currentView_ = this;
		// removeAll();
		initScreenManager(this);

		log_ = new Label(this, "show log", true, false, true);
		show();
		Utils.addManagerAndSetTop(this);
	}

	void show() {
		if (helpInfo_ != null) {
			Utils.parseUsingkXML(helpInfo_, HelpView.this);
		} else {
			downloadHelp();
		}
	}

	void downloadHelp() {
		mid_.waitDialog_.addFgTask(this, new WaitDialog.Task(0) {
			public void run(WaitDialog dlg) throws Exception {
				downloadHelpCatalog(this);
			}
			public void onSuccess(WaitDialog dlg) {
				show();
				super.onSuccess(dlg);
			}
			public void onFailure(WaitDialog dlg) {
				mid_.alert(HelpView.this, getErrMsg(), true);
				super.onFailure(dlg);
			}
		}, false);
		mid_.waitDialog_.setText(ConstantRepository.getWaitText(ConstantRepository.HELPING));
	}

	void downloadHelpCatalog(WaitDialog.Task task) throws Exception {
		StringBuffer url = new StringBuffer(ConfigManager.SERVER_URI);
		url.append("m_help.xml");
		if (null == BaseView.mid_.parser_)
			BaseView.mid_.parser_ = new AtomParser(BaseView.mid_);

		BaseView.mid_.parser_.setParseHelpProperty(AtomParser.parseHelp_, this);
		android.util.Xml.parse(mid_.hm_.sendRequest(url.toString(), task), BaseView.mid_.parser_);
	}

	public void onDestory() {
		activities_.remove(this); // 把当前Activity从集合中移除
	}
}
